﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AnguaRPGEngine.Library.Controls
{
    public partial class GenericListControl : UserControl
    {
        #region Public Enums
        public enum GenericListButtons
        {
            Add,
            Edit,
            Delete
        }
        #endregion
        #region Public Delegates and Events
        public delegate void ButtonClickedEventHandler(GenericListButtons Button, List<int> SelectedItemIndexes);
        public event ButtonClickedEventHandler OnButtonClicked;
        #endregion
        #region Public Properties
        #region ListView
        public ListView ListView
        {
            get
            {
                return listView1;
            }
            set
            {
                listView1 = value;
            }
        }
        #endregion
        #endregion
        #region ctor
        public GenericListControl()
        {
            InitializeComponent();
            btnEdit.Enabled = btnDelete.Enabled = false;
        }
        #endregion
        #region Public Methods
        #endregion
        #region Private Methods
        #endregion
        #region Private Events
        #region listView1_SelectedIndexChanged
        /// <summary>
        /// Fired whenever the listview's selected index is changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnEdit.Enabled = btnDelete.Enabled = (listView1.SelectedItems.Count > 0);
        }
        #endregion
        #region Button_AvailabilityChanged
        /// <summary>
        /// Fired when the buttons become enabled or disabled, changed background image accordingly
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_AvailabilityChanged(object sender, EventArgs e)
        {
            switch (((Button)sender).Name)
            {
                case "btnEdit":
                    ((Button)sender).BackgroundImage = ((Button)sender).Enabled ? Properties.Resources.Edit : Properties.Resources.EditDisabled;
                    break;
                case "btnDelete":
                    ((Button)sender).BackgroundImage = ((Button)sender).Enabled ? Properties.Resources.Delete : Properties.Resources.DeleteDisabled;
                    break;
            }
        }
        #endregion
        #region Button_Click
        /// <summary>
        /// Fired when any button is clicked on the form, tries to raise the OnButtonClicked public event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, EventArgs e)
        {
            if (OnButtonClicked != null)
            {
                GenericListButtons btn = GenericListButtons.Add;
                switch (((Button)sender).Name)
                {
                    case "btnEdit":
                        btn = GenericListButtons.Edit;
                        break;
                    case "btnDelete":
                        btn = GenericListButtons.Delete;
                        break;
                }
                List<int> SelectedItemIndexes = new List<int>();
                foreach (ListViewItem lvi in listView1.SelectedItems)
                {
                    SelectedItemIndexes.Add(lvi.Index);
                }
                OnButtonClicked(btn, SelectedItemIndexes);
            }
        }
        #endregion
        #endregion
    }
}
